/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfocompany.web;
import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfocompany.entity.*;
import com.lczyfz.campusinfo.modules.campusinfocompany.service.*;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 公司管理Controller
 * @author zms
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "api/campusinfocompany/campusinfoCompany")
public class CampusinfoApiCompanyController extends BaseController {

	@Autowired
	private CampusinfoFundReqService campusinfoFundReqService;
	@Autowired
	private LinkCompanyService linkCompanyService;
	@Autowired
	private CampusinfoConsistService campusinfoConsistService;
	@Autowired
	private CompanySearchService companySearchService;
	@RequestMapping(value = "reqList")//获取公司名称
	@ResponseBody
	Map<String, Object> getreqList(CampusinfoFundReq campusinfoFundReq, HttpServletRequest request, HttpServletResponse response) {
		campusinfoFundReq.setUser(UserUtils.getUser());
		CompanySearch companySearch = new CompanySearch();
		CampusinfoConsist campusinfoConsist = new CampusinfoConsist();
		companySearch.setUser(UserUtils.getUser());
		List<CompanySearch> findcompanySearchs=companySearchService.findList(companySearch); //通过权限找到公司
		CompanySearch findcompanySearch = new CompanySearch();
		findcompanySearch.setCompanyId(findcompanySearchs.get(0).getCompanyId()); //从公司找到所以申请的资金
		List<CompanySearch> companySearchs = companySearchService.findList(findcompanySearch);
		campusinfoConsist.setCampanyId(findcompanySearchs.get(0).getCompanyId());
		List<CampusinfoConsist> campusinfoConsists = campusinfoConsistService.findList(campusinfoConsist);
		Page<CampusinfoFundReq> page = campusinfoFundReqService.findPage(new Page<CampusinfoFundReq>(request, response),campusinfoFundReq);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		map.put("page", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("count", page.getCount());
		map.put("totalPage", page.getTotalPage());
		map.put("companySearchs", companySearchs);
		map.put("campusinfoConsists",campusinfoConsists);
		return map;
	}
	@RequestMapping(value = "Reqpower")//用户是否有该公司的权限
	@ResponseBody
	Map<String, Object> Reqpower(LinkCompany linkCompany, HttpServletRequest request, HttpServletResponse response) {
		linkCompany.setUser(UserUtils.getUser());
		List<LinkCompany> linkCompanies=linkCompanyService.findList(linkCompany);
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
		if(linkCompanies.isEmpty()){
			map.put("success",false);
		} else{
			map.put("success",true);
		}
		return map;
	}

	@RequestMapping(value = "addFundReq")//申请资金
	@ResponseBody
	Map<String, Object> addreq(CampusinfoFundReq campusinfoFundReq, HttpServletRequest request, HttpServletResponse response) {
        campusinfoFundReq.setUser(UserUtils.getUser());
		Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if(campusinfoFundReq.getCompanyId() ==""){
			map.put("message","申请资金失败");
		} else{
			campusinfoFundReq.setReqStatus(CampusinfoOrderStat.DONE.getValue());
			campusinfoFundReqService.save(campusinfoFundReq);
			map.put("message","申请资金成功");
		}
		return map;
	}

}